package com.iut.base_de_donnees;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.SparseArray;

public class QuestRepBDD
{
	// Database Version
	private static final int DATABASE_VERSION = 1;
	// Database Name
	private static final String DATABASE_NAME = "QuestRep.db";
	// Contacts table name
	private static final String TABLE_QUESTREP = "QuestionReponse";
	
	// Contacts Table Columns names
	private static final String KEY_ID = "id";
	private static final int NUM_KEY_ID = 0;
	
	private static final String KEY_QUESTION = "question";
	private static final int NUM_KEY_QUESTION = 1;
	
	private static final String KEY_REPONSE_1 = "reponse_1";
	private static final int NUM_KEY_REPONSE_1 = 2;
	
	private static final String KEY_REPONSE_2 = "reponse_2";
	private static final int NUM_KEY_REPONSE_2 = 3;
	
	private static final String KEY_REPONSE_3 = "reponse_3";
	private static final int NUM_KEY_REPONSE_3 = 4;
	
	private static final String KEY_REPONSE_4 = "reponse_4";
	private static final int NUM_KEY_REPONSE_4 = 5;
	
	private static final String KEY_GOOD_REPONSE = "good_reponse";
	private static final int NUM_KEY_GOOD_REPONSE = 6;
	
	private SQLiteDatabase bdd;
	
	private BaseDeDonnee baseDeDonne;
	
	public QuestRepBDD(Context context)
	{
		//On cr�er la BDD et sa table
		baseDeDonne = new BaseDeDonnee(context, DATABASE_NAME, null, DATABASE_VERSION);
	}
	
	public void open()
    {
		//on ouvre la BDD en �criture
		bdd = baseDeDonne.getWritableDatabase();
	}
 
	public void close()
	{
		//on ferme l'acc�s � la BDD
		bdd.close();
	}
 
	public SQLiteDatabase getBDD()
	{
		return bdd;
	}
    
    public long addQuestRep(QuestRep questRep)
    {
    	//Cr�ation d'un ContentValues (fonctionne comme une HashMap)
    	ContentValues values = new ContentValues();
    	//on lui ajoute une valeur associ� � une cl� (qui est le nom de la colonne dans laquelle on veut mettre la valeur)
    	values.put(KEY_QUESTION, questRep.getQuestion());
    	values.put(KEY_REPONSE_1, questRep.getReponse_1());
    	values.put(KEY_REPONSE_2, questRep.getReponse_2());
    	values.put(KEY_REPONSE_3, questRep.getReponse_3());
    	values.put(KEY_REPONSE_4, questRep.getReponse_4());
    	values.put(KEY_GOOD_REPONSE, questRep.getGood_reponse());
    	//on ins�re l'objet dans la BDD via le ContentValues
    	return bdd.insert(TABLE_QUESTREP, null, values);
    }
    
    public int updateQuestRep(int id, QuestRep questRep)
    {
    	//Cr�ation d'un ContentValues (fonctionne comme une HashMap)
    	ContentValues values = new ContentValues();
    	//on lui ajoute une valeur associ� � une cl� (qui est le nom de la colonne dans laquelle on veut mettre la valeur)
    	values.put(KEY_QUESTION, questRep.getQuestion());
    	values.put(KEY_REPONSE_1, questRep.getReponse_1());
    	values.put(KEY_REPONSE_2, questRep.getReponse_2());
    	values.put(KEY_REPONSE_3, questRep.getReponse_3());
    	values.put(KEY_REPONSE_4, questRep.getReponse_4());
    	values.put(KEY_GOOD_REPONSE, questRep.getGood_reponse());
    	//on ins�re l'objet dans la BDD via le ContentValues
    	return bdd.update(TABLE_QUESTREP, values, KEY_ID + " = " +id, null);
    }
    
    public int removeQuestRepWithID(int id)
    {
		//Suppression d'une Question/Reponse de la BDD gr�ce � l'ID
		return bdd.delete(TABLE_QUESTREP, KEY_ID + " = " +id, null);
	}
    
    public int removeQuestRepAll()
    {
    	return bdd.delete(TABLE_QUESTREP,null,null);
    }
    
    public QuestRep getQuestRepWithID(int id)
    {
		//R�cup�re dans un Cursor les valeur correspondant � une Question/Reponse contenu dans la BDD (ici on s�lectionne la Question/Reponse gr�ce � son titre)
		Cursor c = bdd.query(TABLE_QUESTREP, new String[] {KEY_ID, KEY_QUESTION, KEY_REPONSE_1, KEY_REPONSE_2, KEY_REPONSE_3, KEY_REPONSE_4, KEY_GOOD_REPONSE}, KEY_ID + " = "+id, null, null, null, null);
		return cursorToQuestRep(c);
	}
    
    public SparseArray <QuestRep> getAllQuest()
    {
    	Cursor c = bdd.query(TABLE_QUESTREP, new String[] {KEY_ID, KEY_QUESTION, KEY_REPONSE_1, KEY_REPONSE_2, KEY_REPONSE_3, KEY_REPONSE_4, KEY_GOOD_REPONSE}, null, null, null, null, null);
    	return cursorToQuestRepList(c);
    }
    
    private QuestRep cursorToQuestRep(Cursor c)
    {
		//si aucun �l�ment n'a �t� retourn� dans la requ�te, on renvoie null
		if (c.getCount() == 0)
		{return null;}
 
		//Sinon on se place sur le premier �l�ment
		c.moveToFirst();
		//On cr�� un livre
		QuestRep questRep = new QuestRep();
		
		//on lui affecte toutes les infos gr�ce aux infos contenues dans le Cursor
		questRep.setId(c.getInt(NUM_KEY_ID));
		questRep.setQuestion(c.getString(NUM_KEY_QUESTION));
		questRep.setReponse_1(c.getString(NUM_KEY_REPONSE_1));
		questRep.setReponse_2(c.getString(NUM_KEY_REPONSE_2));
		questRep.setReponse_3(c.getString(NUM_KEY_REPONSE_3));
		questRep.setReponse_4(c.getString(NUM_KEY_REPONSE_4));
		questRep.setGood_reponse(c.getInt(NUM_KEY_GOOD_REPONSE));
		//On ferme le cursor
		c.close();
 
		//On retourne la question/reponse
		return questRep;
	}
    
    private SparseArray<QuestRep> cursorToQuestRepList(Cursor c){

        c.moveToFirst();
        SparseArray<QuestRep> apps = new SparseArray<QuestRep>();
        int nb = 0, size = c.getCount();
        while(nb<size)
        {
        	QuestRep questRep = new QuestRep();
        	questRep.setId(c.getInt(NUM_KEY_ID));
    		questRep.setQuestion(c.getString(NUM_KEY_QUESTION));
    		questRep.setReponse_1(c.getString(NUM_KEY_REPONSE_1));
    		questRep.setReponse_2(c.getString(NUM_KEY_REPONSE_2));
    		questRep.setReponse_3(c.getString(NUM_KEY_REPONSE_3));
    		questRep.setReponse_4(c.getString(NUM_KEY_REPONSE_4));
    		questRep.setGood_reponse(c.getInt(NUM_KEY_GOOD_REPONSE));
            apps.put(nb, questRep);
            c.moveToNext();
            nb++;
        }

        c.close();
        return apps;
  }
}
